package logger

import (
	"context"
	"github.com/google/wire"
	"sieve_admin_server_userpass/app/api/util"
	"sieve_admin_server_userpass/app/contextx"
	"sieve_admin_server_userpass/app/schema"
	"sieve_admin_server_userpass/app/server"
	serviceCustomer "sieve_admin_server_userpass/app/service/customer"
	serviceLog "sieve_admin_server_userpass/app/service/logger"
)

var _ util.API = (*LoginAPI)(nil)

var LoginSet = wire.NewSet(wire.Struct(new(LoginAPI), "*"))

type LoginAPI struct {
	util.EmptyAPI
	UserSrv  *serviceCustomer.UserSrv
	LoginSrv *serviceLog.LoginSrv
}

func (a *LoginAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "login",
			Title: "登录日志", Perms: []*server.Perm{
				{Name: "query", Title: "搜索"},
			},
			Order: 8,
		},
	}
}

func (a *LoginAPI) API_query_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("name"),
				contextx.NewVar("start", true),
				contextx.NewVar("end", true),
			},
		)

		p = schema.PaginationParam{
			PageIndex: util.ToInt(ctx.Args.Get("page_index")),
			PageSize:  util.ToInt(ctx.Args.Get("page_size")),
		}
		repKeys = []string{"*"}
	)

	roleId, err := a.UserSrv.GetRoleID(context.Background(), uint64(ctx.Uid))
	if err != nil {
		return err
	}

	return a.LoginSrv.Query(context.Background(), uint64(ctx.Uid), roleId, reqArgs, p, repKeys)
}
